﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Threading;
using System.IO;
using System.Diagnostics;

namespace Gesture_Manager
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Label> label_bentStatus = new List<Label>();
        private List<Label> label_bentStatusExpectation = new List<Label>();
        private static MainWindow thisWindow;
        
        public MainWindow()
        {
            InitializeComponent();
            var filePath = Environment.CurrentDirectory + "\\palm.png";
            image_palm.Source = new BitmapImage(new Uri(filePath));

            for(int i = 0; i < 5; i++)
            {
                for(int j = 0; j < 2; j++)
                {
                    Label newLabel_CurrentData = new Label();
                    
                    newLabel_CurrentData.Background = (Brush)new BrushConverter().ConvertFromString("#FFDDDDDD");
                    newLabel_CurrentData.Foreground = new SolidColorBrush(Colors.Black);
                    
                    newLabel_CurrentData.Margin = new Thickness(60+i * 80, j * 150, 0, 0);
                    newLabel_CurrentData.VerticalAlignment = VerticalAlignment.Top;
                    newLabel_CurrentData.HorizontalAlignment = HorizontalAlignment.Left;
                    newLabel_CurrentData.Width = 40;
                    newLabel_CurrentData.Height = 30;

                    newLabel_CurrentData.Content = "0";
                    newLabel_CurrentData.HorizontalContentAlignment = HorizontalAlignment.Center;

                    grid_main_window.Children.Add(newLabel_CurrentData);
                    label_bentStatus.Add(newLabel_CurrentData);

                    Label newLabel_ExpectedData = new Label();

                    newLabel_ExpectedData.Background = (Brush)new BrushConverter().ConvertFromString("#FF82FFCB");
                    newLabel_ExpectedData.Foreground = new SolidColorBrush(Colors.Black);

                    newLabel_ExpectedData.Margin = new Thickness(60 + i * 80, j * 150+30, 0, 0);
                    newLabel_ExpectedData.VerticalAlignment = VerticalAlignment.Top;
                    newLabel_ExpectedData.HorizontalAlignment = HorizontalAlignment.Left;
                    newLabel_ExpectedData.Width = 40;
                    newLabel_ExpectedData.Height = 30;

                    newLabel_ExpectedData.Content = "0";
                    newLabel_ExpectedData.HorizontalContentAlignment = HorizontalAlignment.Center;

                    grid_main_window.Children.Add(newLabel_ExpectedData);
                    label_bentStatusExpectation.Add(newLabel_ExpectedData);
                }
            }
            StartUpdatingInfo();            
        }


        //updating data on the UI
        Thread thread_Updating = new Thread(new ThreadStart(() => {
            CoreDetectionManager.SampleMain();
            thisWindow.Dispatcher.Invoke(new Action(()=> {
                thisWindow.combo_Categories.ItemsSource = ConfigManager.list_Categories;                
            }));
        }));
        private void StartUpdatingInfo()
        {
            thisWindow = this;
            CoreDetectionManager.delegate_fetcher = new CoreDetectionManager.delegate_HandInfoFetcher(UpdateInfo);
            CoreDetectionManager.delegate_sender = new CoreDetectionManager.delegate_RecognitionInfoSender(UpdateRecognizedInfo);
            thread_Updating.Start();
            //CoreDetectionManager.SampleMain();


        }
        public void UpdateExpectationInfo(SignLanguageConfig config)
        {
            for (int i = 0; i < 10; i++)
            {
                label_bentStatusExpectation[i].Content = config.BentPercentExpectation[i / 2, i % 2];
            }
        }
        public void UpdateInfo(HandSimpled hand)
        {
            //put hand info onto display
            this.Dispatcher.Invoke(new Action(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    label_bentStatus[i].Content = hand.bentPercent[i / 2, i % 2];
                }
            }));
        }

        public void UpdateRecognizedInfo(SignLanguageConfig config)
        {
            //put recognition info onto display
            this.Dispatcher.Invoke(new Action(() => {
                label_CurrentRecognition.Content = config.ToString();
            }));
        }

        private void combo_Categories_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            list_Configs.ItemsSource = ConfigManager.GetHandFromCategory((string)((ComboBox)sender).SelectedItem);
        }

        private void combo_Categories_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (((ComboBox)sender).Items.Count > 0) ((ComboBox)sender).SelectedItem = ((ComboBox)sender).Items[0];
        }

        private void button_AddConfig_Click(object sender, RoutedEventArgs e)
        {
            string newCategory = textBox_NewCategory.Text;
            ConfigManager.list_Categories.Add(newCategory);
            Directory.CreateDirectory(Environment.CurrentDirectory + "\\configs\\" + textBox_NewCategory.Text);
        }

        private void combo_Categories_SourceUpdated(object sender, DataTransferEventArgs e)
        {
            if (((ComboBox)sender).Items.Count > 0) ((ComboBox)sender).SelectedItem = ((ComboBox)sender).Items[0];
        }

        private void list_Configs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox thisListBox = (ListBox)sender;
            UpdateExpectationInfo((SignLanguageConfig)thisListBox.SelectedItem);
            Debug.WriteLine(((SignLanguageConfig)thisListBox.SelectedItem).GetPath());
        }

        private void button_NewConfig_Click(object sender, RoutedEventArgs e)
        {
            ConfigManager.AddConfig((string)combo_Categories.SelectedItem, textBox_NewConfigName.Text);
            
        }
    }
}
